package com.demo.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @描述  kafka生成数据
 * @创建人 zengqingdong
 * @创建时间 2020/6/15 14:56
 */
public class KafkaProducerDemo {

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", "8.134.126.106:9092,8.134.126.106:9093,8.134.126.106:9094");
        properties.put("serializer.class", "kafka.serializer.StringEncoder");
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
        try {
            for (int i = 0; i < 100; i++) {
                Thread.sleep(1000);
                producer.send(new ProducerRecord<String, String>("test1", "这是我的测试"+i));
            }
            System.out.println("send success from producer");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            producer.close();
        }
    }
}
